package com.itheima.exception;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.support.spring.FastJsonJsonView;
import com.itheima.entity.Result;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyExceptionResolver implements HandlerExceptionResolver {
@Override
public ModelAndView resolveException(HttpServletRequest request,
                                     HttpServletResponse response, Object handler, Exception ex) {
    //处理异常的代码实现
    //创建ModelAndView对象
    FastJsonJsonView view = new FastJsonJsonView();
    ModelAndView modelAndView = new ModelAndView();
    Result result = (Result) request.getAttribute("result");
    if (result!=null){
        modelAndView.setView(view);
        modelAndView.addObject("flag",result.isFlag());
        modelAndView.addObject("message",result.getMessage());
    }
    return modelAndView;
    }
}